package com.kimho.product.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kimho.core.PageInfo;
import com.kimho.product.domain.Product;
import com.kimho.product.persistence.ProductMapper;

@Service
public class ProductService {
	@Autowired
	private ProductMapper productMapper;

	public ProductMapper getProductMapper() {
		return productMapper;
	}

	public void setProductMapper(ProductMapper productMapper) {
		this.productMapper = productMapper;
	}
	
	public List<Product> getAllProduct(PageInfo pi){
		return productMapper.getAllProduct(pi);
	}
	
	
	public List<Product> getAllProduct_s(PageInfo pi){
		return productMapper.getAllProduct_s(pi);
	}
	public Long getTotal_s(PageInfo pi){
		return this.productMapper.getTotal(pi);
	}
	public Long getTotal(PageInfo pi){
		return this.productMapper.getTotal(pi);
	}
	@Transactional(rollbackFor=Exception.class)
	public void deletelogobj(String [] ids ){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("over", "1");//over字段为1表示该物件未纳入流水单
		for (int i = 0; i < ids.length; i++) {
			map.put("object_id", ids[i]);
			this.productMapper.deletelogobj(map);
			this.productMapper.updateObject_det_over(map);
		}
	}
	@Transactional(rollbackFor=Exception.class)
	public void savelogobj(String [] object_ids,String logistics_id){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("logistics_id", logistics_id);
		map.put("over", "2");//over字段为2表示该物件纳入流水单
		for (int i = 0; i < object_ids.length; i++) {
			map.put("object_id", object_ids[i]);
			this.productMapper.savelogobj(map);
			this.productMapper.updateObject_det_over(map);//更新object_det表的over字段为2
		}
	}
	@Transactional(rollbackFor=Exception.class)
	public void deletelogobjbylogid(String [] ids ){
		Map<String, Object> map = new HashMap<String, Object>();
		for (int i = 0; i < ids.length; i++) {
			map.put("logistics_id", ids[i]);
			map.put("over", "1");//over字段为1表示该物件未纳入流水单
			List<String> object_ids = this.productMapper.getObject_idByLogistics_id(map);
			this.productMapper.deletelogistics(map);
			this.productMapper.deletelogobj(map);
			for (String object_id : object_ids) {
				map.put("object_id", object_id);
				this.productMapper.updateObject_det_over(map);//更新object_det表的over字段为1
			}
		}
	}
	@Transactional(rollbackFor=Exception.class)
	public void save(Map<String, Object> param) {
		this.productMapper.save(param);
	}
	@Transactional(rollbackFor=Exception.class)
	public void update(Map<String, Object> param){
		this.productMapper.update(param);
	}
	
	public List<Map<String, String>> getTruckerByPost_id(String post_id){
		return this.productMapper.getTruckerByPost_id(post_id);
	}
	/**
	 *  发车
	 */
	@Transactional
	public void startCar(String logistics_id){
		this.productMapper.startCar(logistics_id);
	}
}
